#include "airflow_menu.h"
#include "gasdilute_menu.h"
#include "seto3_menu.h"
Airflow_Menu::Airflow_Menu(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Init();
    connect_Init();

    if((!O3_Show_Flag))
    {
if(btnlist_ui.at(2)->objectName()=="O3")
{
   btnlist_ui.at(2)->hide();
}
    }

    //是否启用光度计模式时按键布局的变化
      if((!Photometer_Flag))
      {//当光度计不启用并且光度计未隐藏并且臭氧未隐藏
          if(btnlist_ui.at(3)->objectName()=="光度计")
          {
             btnlist_ui.at(3)->hide();
          }
      }
}

void Airflow_Menu::ui_Init()
{



    the_btn_style[0]=("\
                      outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                      background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                      border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                      border-style: outset;\
                      /* 按钮背景图标设置 */\
                      background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                      background-origin: content;\
                      background-position: top;	         /* 背景图片的位置 */\
                      padding-right: 0px;	    /*内边距设置*/	    \
                      padding-left: 0px;		/*  */	\
                      padding-bottom: 10px;		/*  */    \
                      padding-top: 20px;		/*  */    \
                      background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                      /* 按钮文本设置 */\
                      text-align: bottom;			             /* 文本的位置 */\
                      font-size: 20px;\
                      color: #FFFFFF;			                 /* 文本颜色 */\
                 "
);

//
the_btn_style[2]=("QPushButton:pressed{background-color:rgb(99,211,133);}");
the_btn_style[1]=("\
                  outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                  border-radius: 8px;		                 /* 按钮边框的圆角设置 */\
                  border-style: outset;\
                  /* 按钮背景图标设置 */\
                  background-image: url(:/pic/Airflow_mode/等待.png); /* 背景图片 */\
                  background-origin: content;\
                  background-position: top;	         /* 背景图片的位置 */\
                  padding-right: 0px;	    /*内边距设置*/	    \
                  padding-left: 0px;		/*  */	\
                  padding-bottom: 10px;	/*  */   \
                  padding-top:10px;		/*  */    \
                  background-repeat: repeat;	         /* 设置背景图像的平铺模式 */\
                  /* 按钮文本设置 */\
                  text-align:bottom ;			             /* 文本的位置 */\
                  font-size: 22px;\
                  color: rgb(225, 225, 225);			                 /* 文本颜色 */\
                  ");
    for(int i=0;i<5;i++)//创建主屏幕显示的按键
    {
         theBtn[i] = new QPushButton(this);
//        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
        theBtn[i]->setFixedSize(112,84);
        theBtn[i]->geometry();

btnlist_ui.append(theBtn[i]);

}

    for(int i=0;i<2;i++)//创建主屏幕显示的按键
    {
         theLabel[i] = new QLabel(this);
    }
    theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
    theLabel[1]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
   theLabel[0]->setGeometry(0,0,150,60);//设定每隔放置一个按键
   theLabel[1]->setGeometry(150,0,490,60);//设定每隔放置一个按键
   theLabel[1]->setText("气体设置           ");
theBtn[0]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(10, 50, 255);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/等待.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(10, 50, 255);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/等待_2.png); /* 背景图片 */"+"}");
theBtn[1]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(220, 110, 255);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/气体稀释.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(220, 110, 255);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/气体稀释_2.png); /* 背景图片 */"+"}");
theBtn[2]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(220, 50, 105);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/臭氧.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(220, 50, 105);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/臭氧.2png.png); /* 背景图片 */"+"}");
theBtn[3]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(250, 180, 22);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/光度计.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(250, 180, 22);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/光度计2.png); /* 背景图片 */"+"}");
theBtn[4]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(60, 255, 152);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(60, 255, 152);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Airflow_mode/返回2.png); /* 背景图片 */"+"}");
btnlist_ui.at(0)->setText(tr("等待"));
btnlist_ui.at(1)->setText(tr("气体稀释"));
btnlist_ui.at(2)->setText(tr("O3"));
btnlist_ui.at(2)->setObjectName(tr("O3"));
btnlist_ui.at(3)->setText(tr("光度计"));
btnlist_ui.at(3)->setObjectName(tr("光度计"));
btnlist_ui.at(4)->setText(tr("返回"));
//btnlist_ui.at(4)->setGeometry(30,430,100,50 );//设定每隔放置一个按键

QGridLayout *theLayout = new QGridLayout();
theLayout->addWidget(btnlist_ui.at(0), 1, 0);
theLayout->addWidget(btnlist_ui.at(1), 1, 1);
theLayout->addWidget(btnlist_ui.at(2), 1, 2);
theLayout->addWidget(btnlist_ui.at(3), 1, 3);
theLayout->addWidget(btnlist_ui.at(4), 1, 4);
//theLayout->setVerticalSpacing(50);
//theLayout->setHorizontalSpacing(10);
theLayout->setContentsMargins(0,120,0,0);//(int left, int top, int right, int bottom)
//theLayout->setColumnStretch(1,22);
theLayout->setSizeConstraint(QLayout::SetMinimumSize);
  setLayout(theLayout);

  QWidget* widget = new QWidget(this);
  widget->setGeometry(0,0,640,330);
  widget->setLayout(theLayout);

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}




}
void Airflow_Menu::connect_Init()
{
    //等待
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        Wait  *menux = new Wait(this);
        menux->show();

    });

    //气体稀释
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        Gasdilute_Menu  *menux = new Gasdilute_Menu(this);
        menux->show();

    });
    //臭氧设置
    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){
        Seto3_Menu  *menux = new Seto3_Menu(this);
        menux->show();
    });

    //光度计设置
    connect(btnlist_ui.at(3),&QPushButton::clicked,[=](){
        Photometer_Menu  *menux = new Photometer_Menu(this);
        menux->show();
    });
    //返回
    connect(btnlist_ui.at(4),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });
}
bool Airflow_Menu::eventFilter(QObject *watched, QEvent *event)
{

//            key_Event::key_Event1(pos,watched, event, btnlist, btnlist_ui);
        for(int i=0;i<btnlist_ui.length();i++)
            if(btnlist_ui.at(i) == watched)
        {
            if(event->type() == QEvent::KeyPress)
            {
               QKeyEvent *ev = (QKeyEvent *)event;
               QPushButton *button = (QPushButton *)watched;
               switch (ev->key())
               {
               case OK:
                 {
                   emit  button->clicked();
                   break;
                 }
               case UP://上
               {
                   if(focusWidget() != btnlist_ui.first())
                   focusPreviousChild();
                   focusPreviousChild();
                   focusPreviousChild();
                   focusPreviousChild();
                   break;
               }
               case DOWN://下
               {
                   if(focusWidget() != btnlist_ui.back())
                   focusNextChild();
                   focusNextChild();
                   focusNextChild();
                   focusNextChild();
                   break;
               }
               case LEFT://左
               {
                 if(focusWidget() != btnlist_ui.first())
                      focusPreviousChild();
                 break;
               }
               case RIGHT://右
               {
                 if(focusWidget() != btnlist_ui.back())
                      focusNextChild();
              break;
               }
               case INIT: //打开初始化界面
               {
                   init_Show *show = new init_Show(this);
                   show->show();
                   this->hide();
                   break;
               }
               case BACK:
               {
                   this->parentWidget()->show();
                   this->close();
                   break;
               }
               default:
                   break;
               }
            }
            else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
                      {
                          QPushButton *focus_button = (QPushButton *)watched;
                                  focus_button->setFixedSize(132,99);
//                                  focus_button->setStyleSheet(focusBtn[2]->styleSheet());
//                                  focus_button->setIconSize(QSize(1,2));
                      }
            else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
                     {
                QPushButton *focus_button = (QPushButton *)watched;
                        focus_button->setFixedSize(112,84);
                     }
            }
}

